{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "自动备份最近7次观测太阳本地文件到37网盘程序,\n",
    "文件已存在的不在进行备份，所以要今天观测完后在进行备份，\n",
    "\n",
    "重要注意事项：\n",
    "WL，49这台电脑，处理目录在\"\\\\10.153.110.37\\Data\\太阳观测\\2021\\wl\",\n",
    "所以需要把需要处理的文档（未改名）放入上方地址即可，处理完后删除刚刚放入的文件夹，已保持文件夹整洁\n",
    "处理完WL 会报错，但是不影响结果，忽略即可\n",
    "\n",
    "否则无法备份已生成文件夹的日期文件，解决办法是删除掉当天不小心备份的37网盘文件夹\n",
    "直接运行，报错请截图联系QQ:471238751\n",
    "后期增加每日自动检查备份功能（无法实现）\n",
    "文件备份速度慢，会导致程序宕机，甚至无法执行下一天文件备份\n",
    "\n",
    "已发现BUG\n",
    "处理完某天日期后程序不动了\n",
    "2种处理方法：\n",
    "1.点击最上面菜单👆运行右边第二个按钮（正方形右边那个）\n",
    "稍等程序重启后在点击运行进行启动程序\n",
    "2.再次运行程序，但是请确保上一个程序处理的日期备份完毕，否则新运行的程序不会自动补充未备份\n",
    "的图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z:盘最近10次观测的时间列表\n",
      "20210218\n",
      "20210218\n",
      "20210217\n",
      "20210217\n",
      "20210216\n",
      "20210216\n",
      "20210215\n",
      "20210215\n",
      "20210214\n",
      "20210214\n",
      "正在检查是否上传至37网盘\n",
      "20210218文件还未备份，开始创建备份\n",
      "20210218文件还未备份，开始创建备份\n",
      "20210217文件还未备份，开始创建备份\n",
      "20210217文件还未备份，开始创建备份\n",
      "20210216文件还未备份，开始创建备份\n",
      "20210216文件还未备份，开始创建备份\n",
      "20210215文件还未备份，开始创建备份\n",
      "20210215文件还未备份，开始创建备份\n",
      "20210214文件还未备份，开始创建备份\n",
      "20210214文件还未备份，开始创建备份\n",
      "开始备份1，请稍后\n",
      "20210215_013421\n",
      "20210215备份完成\n",
      "开始备份1，请稍后\n",
      "20210215_062007\n",
      "20210215备份完成\n",
      "开始备份1，请稍后\n",
      "20210218_061917\n",
      "20210218备份完成\n",
      "开始备份1，请稍后\n",
      "20210218_012204\n",
      "20210218备份完成\n",
      "开始备份1，请稍后\n",
      "20210214_014906\n",
      "20210214备份完成\n",
      "开始备份1，请稍后\n",
      "20210214_065050\n",
      "20210214备份完成\n",
      "开始备份1，请稍后\n",
      "20210217_062754\n",
      "20210217备份完成\n",
      "开始备份1，请稍后\n",
      "20210217_015017\n",
      "20210217备份完成\n",
      "开始备份1，请稍后\n",
      "20210216_063418\n",
      "20210216备份完成\n",
      "开始备份1，请稍后\n",
      "20210216_012720\n",
      "20210216备份完成\n",
      "\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl盘最近10次观测的时间列表\n",
      "20210218\n",
      "20210217\n",
      "20210217\n",
      "20210216\n",
      "20210216\n",
      "20210215\n",
      "20210215\n",
      "20210208\n",
      "正在检查是否上传至37网盘\n",
      "20210218文件还未备份，开始创建备份\n",
      "20210217文件还未备份，开始创建备份\n",
      "20210217文件还未备份，开始创建备份\n",
      "20210216文件还未备份，开始创建备份\n",
      "20210216文件还未备份，开始创建备份\n",
      "20210215文件还未备份，开始创建备份\n",
      "20210215文件还未备份，开始创建备份\n",
      "20210208文件还未备份，开始创建备份\n",
      "开始备份1，请稍后\n",
      "20210215_013501\n",
      "20210215备份完成\n",
      "开始备份1，请稍后\n",
      "20210215_062025\n",
      "20210215备份完成\n",
      "开始备份1，请稍后\n",
      "20210218_012318\n",
      "20210218备份完成\n",
      "开始备份1，请稍后\n",
      "20210208_011010\n",
      "20210208备份完成\n",
      "开始备份1，请稍后\n",
      "20210217_062941\n",
      "20210217备份完成\n",
      "开始备份1，请稍后\n",
      "20210217_014949\n",
      "20210217备份完成\n",
      "开始备份1，请稍后\n",
      "20210216_012752\n",
      "20210216备份完成\n",
      "开始备份1，请稍后\n",
      "20210216_063521\n",
      "20210216备份完成\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import datetime,shutil\n",
    "pan = ['Z','X']\n",
    "for p in pan:\n",
    "    if p == 'X': #第一次检查 49 wl电脑文件\n",
    "        Y = r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl'\n",
    "        YY ='WL'\n",
    "        path37 =r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl'\n",
    "    elif p == 'Z':  #第二次检查 46 cak电脑文件\n",
    "        Y = r'Z:'\n",
    "        YY = 'CAK'\n",
    "        path37 = r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\cak'\n",
    "        \n",
    "    filelie=[]#文件列表\n",
    "    for root,dirs,files in os.walk(Y):\n",
    "        for i in dirs:\n",
    "            if len(i) == 13:  #针对10/20之前的文件命名\n",
    "                times ='20'+i[4:6]+i[2:4]+i[0:2]\n",
    "                #times=datetime.datetime.strptime(times,'%Y%m%d')\n",
    "                filelie.append(times)\n",
    "            elif len(i) >13:  #新的文件命名方式\n",
    "                filelie.append(i[:8])\n",
    "    #对文件夹进行排列\n",
    "    filelie.sort(reverse=True)\n",
    "    \n",
    "    file7 =[] #输出最近7天观测时间 20201203\n",
    "    file7bf=[] #备份文件名称 20201203\n",
    "\n",
    "    #如需修改观测时间列表长度，则修改for i in filelie[:7]尾部[:7]的7，需要改成10则是[:10]\n",
    "    print(Y +'盘最近10次观测的时间列表')\n",
    "    for i in filelie[:10]:\n",
    "        file7.append(i)\n",
    "        print(i)\n",
    "    print('正在检查是否上传至37网盘')\n",
    "    for i in file7:\n",
    "        if os.path.exists(os.path.join(path37,i+YY)):\n",
    "            print(i+'存在，不再进行备份')  #20201203\n",
    "        else:\n",
    "            print(i+'文件还未备份，开始创建备份')\n",
    "            file7bf.append(i)  #20201203\n",
    "    \n",
    "    os.chdir(Y)\n",
    "    for bf in set(file7bf):  #获得需要备份文件夹名称\n",
    "        for i in set(os.scandir()): #获得原本文件夹名称\n",
    "            if bf in i.name:  #如果备份文件夹名称在原本文件夹名称 列表内，则\n",
    "                try:\n",
    "                    print('开始备份1，请稍后')\n",
    "                    print(i.name)\n",
    "                    move = os.path.join(Y,i.name)  #获得原本地址\n",
    "                    if i.name[:4] == '2020':\n",
    "                        yy=YY.lower()\n",
    "                        #地址 = r'\\\\10.153.110.37\\Data\\solar_observation\\2020\\'+ yy\n",
    "                        地址 = os.path.join(r'\\\\10.153.110.37\\Data\\solar_observation\\2020',yy)\n",
    "                    elif i.name[:4] == '2021':\n",
    "                        地址 = path37\n",
    "                        #print(path37+'\\\\'+i.name[:8]+YY)   \\\\10.153.110.37\\Data\\太阳观测\\2020\\cak\\20201024CAK\n",
    "                    shutil.copytree(move,地址+'\\\\'+i.name[:8]+YY) #开始备份\n",
    "                    print(i.name[:8]+'备份完成')\n",
    "                except FileExistsError:  \n",
    "                    for filepath in os.scandir(os.path.join(Y,i.name)):\n",
    "                        shutil.copy2(filepath.path,path37+'\\\\'+i.name[:8]+YY)\n",
    "                    print(i.name[:8]+'备份完成')\n",
    "                except SameFileError:\n",
    "                    pass\n",
    "                \n",
    "            elif bf[6:8]+bf[4:6]+bf[2:4] in i.name: #针对2020年10月24日之前的文件命名\n",
    "                try:\n",
    "                    print('开始备份2，请稍后')\n",
    "                    move = os.path.join(Y,i.name)  #获得原本地址\n",
    "                    shutil.copytree(move,path37+'\\\\'+bf+YY) #开始备份\n",
    "                    print('备份完成')\n",
    "                except:\n",
    "                    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获得本地文件列表\n",
    "# 获得网盘文件列表\n",
    "# 列表差为缺失文件\n",
    "# 获得缺失文件地址，移动到 网盘文件列表中\n",
    "# ha =[]\n",
    "# ha37 =[]\n",
    "# os.chdir(r'y:')\n",
    "# for i in os.scandir(): #获得本地文件名\n",
    "#     print(i.name)\n",
    "    #for a,b,c in os.walk(Y+i):\n",
    "        #print(a)\n",
    "# for c in os.scandir(os.path.join(wlpath,i.name+'WL')): #获得网盘相同目录文件名\n",
    "#     ha37.append(c.name)\n",
    "\n",
    "# diff = set(ha).difference(set(ha37)) #通过差值找到 不相同的文件\n",
    "# print(ha37)\n",
    "##print(ha)\n",
    "# print(diff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.chdir(Y)\n",
    "bf = '20201027'\n",
    "for a,b,c in os.walk(r'y:'): #获得原本文件夹名称\n",
    "    if bf in a:\n",
    "        print(a)\n",
    "#     for i in b:\n",
    "#         if b in bf:\n",
    "#             print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl盘最近7次观测的时间列表\n",
      "20210131\n",
      "20210131\n",
      "20210130\n",
      "20210130\n",
      "20210129\n",
      "20210129\n",
      "20210128\n",
      "正在检查是否上传至37网盘\n",
      "20210131存在，不再进行备份\n",
      "20210131存在，不再进行备份\n",
      "20210130存在，不再进行备份\n",
      "20210130存在，不再进行备份\n",
      "20210129存在，不再进行备份\n",
      "20210129存在，不再进行备份\n",
      "20210128存在，不再进行备份\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import datetime,shutil\n",
    "pan = ['X,Z']\n",
    "for p in pan:\n",
    "    if p == 'X': #第一次检查 49 wl电脑文件\n",
    "        Y = r'y:'\n",
    "        YY ='WL'\n",
    "        path37 =r'\\\\10.153.110.37\\Data\\solar_observation\\2020\\wl'\n",
    "    elif p == 'Z':  #第二次检查 46 cak电脑文件\n",
    "        Y = r'Z:'\n",
    "        YY = 'CAK'\n",
    "        path37 = r'\\\\10.153.110.37\\Data\\solar_observation\\2020\\cak'\n",
    "        \n",
    "    filelie=[]#文件列表\n",
    "    for root,dirs,files in os.walk(Y):\n",
    "        for i in dirs:\n",
    "            if len(i) == 13:  #针对10/20之前的文件命名\n",
    "                times ='20'+i[4:6]+i[2:4]+i[0:2]\n",
    "                #times=datetime.datetime.strptime(times,'%Y%m%d')\n",
    "                filelie.append(times)\n",
    "            elif len(i) >13:  #新的文件命名方式\n",
    "                filelie.append(i[:8])\n",
    "    #对文件夹进行排列\n",
    "    filelie.sort(reverse=True)\n",
    "    #输出最近7天观测时间\n",
    "    file7 =[]\n",
    "    file7bf=[]\n",
    "\n",
    "    #如需修改观测时间列表长度，则修改for i in filelie[:7]尾部[:7]的7，需要改成10则是[:10]\n",
    "    print(Y +'盘最近7次观测的时间列表')\n",
    "    for i in filelie[:7]:\n",
    "        file7.append(i)\n",
    "        print(i)\n",
    "    print('正在检查是否上传至37网盘')\n",
    "    for i in file7:\n",
    "        if os.path.exists(os.path.join(path37,i+YY)):\n",
    "            print(i+'存在，不再进行备份')\n",
    "        else:\n",
    "            print(i+'文件还未备份，开始创建备份')\n",
    "            file7bf.append(i)\n",
    "            \n",
    "    for bf in file7bf:  #获得需要备份文件夹名称\n",
    "        for a,b,c in os.walk(Y): #获得原本文件夹名称\n",
    "            if bf in a:  #如果备份文件夹名称在原本文件夹名称 列表内，则\n",
    "                try:\n",
    "                    print('开始备份，请稍后')\n",
    "                    shutil.copytree(a,path37+'\\\\'+bf+YY) #开始备份\n",
    "                except FileExistsError:\n",
    "                    for filepath in os.scandir(a):\n",
    "                        shutil.copy2(filepath.path,path37+'\\\\'+bf+YY)\n",
    "                    print('备份完成')\n",
    "                    # a = y:Sun\\20201027_014029\n",
    "            elif bf[6:8]+bf[4:6]+bf[2:4] in a: #针对2020年10月24日之前的文件命名\n",
    "                print('开始备份，请稍后')\n",
    "                shutil.copytree(a,path37+'\\\\'+bf+YY) #开始备份\n",
    "                print('备份完成')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "import os\n",
      "import datetime,shutil\n",
      "pan = ['Z','X']\n",
      "for p in pan:\n",
      "    if p == 'X': #第一次检查 49 wl电脑文件\n",
      "        Y = r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl'\n",
      "        YY ='WL'\n",
      "        path37 =r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl'\n",
      "    elif p == 'Z':  #第二次检查 46 cak电脑文件\n",
      "        Y = r'Z:'\n",
      "        YY = 'CAK'\n",
      "        path37 = r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\cak'\n",
      "        \n",
      "    filelie=[]#文件列表\n",
      "    for root,dirs,files in os.walk(Y):\n",
      "        for i in dirs:\n",
      "            if len(i) == 13:  #针对10/20之前的文件命名\n",
      "                times ='20'+i[4:6]+i[2:4]+i[0:2]\n",
      "                #times=datetime.datetime.strptime(times,'%Y%m%d')\n",
      "                filelie.append(times)\n",
      "            elif len(i) >13:  #新的文件命名方式\n",
      "                filelie.append(i[:8])\n",
      "    #对文件夹进行排列\n",
      "    filelie.sort(reverse=True)\n",
      "    \n",
      "    file7 =[] #输出最近7天观测时间 20201203\n",
      "    file7bf=[] #备份文件名称 20201203\n",
      "\n",
      "    #如需修改观测时间列表长度，则修改for i in filelie[:7]尾部[:7]的7，需要改成10则是[:10]\n",
      "    print(Y +'盘最近10次观测的时间列表')\n",
      "    for i in filelie[:10]:\n",
      "        file7.append(i)\n",
      "        print(i)\n",
      "    print('正在检查是否上传至37网盘')\n",
      "    for i in file7:\n",
      "        if os.path.exists(os.path.join(path37,i+YY)):\n",
      "            print(i+'存在，不再进行备份')  #20201203\n",
      "        else:\n",
      "            print(i+'文件还未备份，开始创建备份')\n",
      "            file7bf.append(i)  #20201203\n",
      "    \n",
      "    os.chdir(Y)\n",
      "    for bf in set(file7bf):  #获得需要备份文件夹名称\n",
      "        for i in set(os.scandir()): #获得原本文件夹名称\n",
      "            if bf in i.name:  #如果备份文件夹名称在原本文件夹名称 列表内，则\n",
      "                try:\n",
      "                    print('开始备份1，请稍后')\n",
      "                    print(i.name)\n",
      "                    move = os.path.join(Y,i.name)  #获得原本地址\n",
      "                    if i.name[:4] == '2020':\n",
      "                        yy=YY.lower()\n",
      "                        #地址 = r'\\\\10.153.110.37\\Data\\solar_observation\\2020\\'+ yy\n",
      "                        地址 = os.path.join(r'\\\\10.153.110.37\\Data\\solar_observation\\2020',yy)\n",
      "                    elif i.name[:4] == '2021':\n",
      "                        地址 = path37\n",
      "                        #print(path37+'\\\\'+i.name[:8]+YY)   \\\\10.153.110.37\\Data\\太阳观测\\2020\\cak\\20201024CAK\n",
      "                    shutil.copytree(move,地址+'\\\\'+i.name[:8]+YY) #开始备份\n",
      "                    print(i.name[:8]+'备份完成')\n",
      "                except FileExistsError:  \n",
      "                    for filepath in os.scandir(os.path.join(Y,i.name)):\n",
      "                        shutil.copy2(filepath.path,path37+'\\\\'+i.name[:8]+YY)\n",
      "                    print(i.name[:8]+'备份完成')\n",
      "                except SameFileError:\n",
      "                    pass\n",
      "                \n",
      "            elif bf[6:8]+bf[4:6]+bf[2:4] in i.name: #针对2020年10月24日之前的文件命名\n",
      "                try:\n",
      "                    print('开始备份2，请稍后')\n",
      "                    move = os.path.join(Y,i.name)  #获得原本地址\n",
      "                    shutil.copytree(move,path37+'\\\\'+bf+YY) #开始备份\n",
      "                    print('备份完成')\n",
      "                except:\n",
      "                    pass\n",
      "# 获得本地文件列表\n",
      "# 获得网盘文件列表\n",
      "# 列表差为缺失文件\n",
      "# 获得缺失文件地址，移动到 网盘文件列表中\n",
      "# ha =[]\n",
      "# ha37 =[]\n",
      "# os.chdir(r'y:')\n",
      "# for i in os.scandir(): #获得本地文件名\n",
      "#     print(i.name)\n",
      "    #for a,b,c in os.walk(Y+i):\n",
      "        #print(a)\n",
      "# for c in os.scandir(os.path.join(wlpath,i.name+'WL')): #获得网盘相同目录文件名\n",
      "#     ha37.append(c.name)\n",
      "\n",
      "# diff = set(ha).difference(set(ha37)) #通过差值找到 不相同的文件\n",
      "# print(ha37)\n",
      "##print(ha)\n",
      "# print(diff)\n",
      "os.chdir(Y)\n",
      "bf = '20201027'\n",
      "for a,b,c in os.walk(r'y:'): #获得原本文件夹名称\n",
      "    if bf in a:\n",
      "        print(a)\n",
      "#     for i in b:\n",
      "#         if b in bf:\n",
      "#             print(b)\n",
      "import os\n",
      "import datetime,shutil\n",
      "pan = ['X,Z']\n",
      "for p in pan:\n",
      "    if p == 'X': #第一次检查 49 wl电脑文件\n",
      "        Y = r'y:'\n",
      "        YY ='WL'\n",
      "        path37 =r'\\\\10.153.110.37\\Data\\solar_observation\\2020\\wl'\n",
      "    elif p == 'Z':  #第二次检查 46 cak电脑文件\n",
      "        Y = r'Z:'\n",
      "        YY = 'CAK'\n",
      "        path37 = r'\\\\10.153.110.37\\Data\\solar_observation\\2020\\cak'\n",
      "        \n",
      "    filelie=[]#文件列表\n",
      "    for root,dirs,files in os.walk(Y):\n",
      "        for i in dirs:\n",
      "            if len(i) == 13:  #针对10/20之前的文件命名\n",
      "                times ='20'+i[4:6]+i[2:4]+i[0:2]\n",
      "                #times=datetime.datetime.strptime(times,'%Y%m%d')\n",
      "                filelie.append(times)\n",
      "            elif len(i) >13:  #新的文件命名方式\n",
      "                filelie.append(i[:8])\n",
      "    #对文件夹进行排列\n",
      "    filelie.sort(reverse=True)\n",
      "    #输出最近7天观测时间\n",
      "    file7 =[]\n",
      "    file7bf=[]\n",
      "\n",
      "    #如需修改观测时间列表长度，则修改for i in filelie[:7]尾部[:7]的7，需要改成10则是[:10]\n",
      "    print(Y +'盘最近7次观测的时间列表')\n",
      "    for i in filelie[:7]:\n",
      "        file7.append(i)\n",
      "        print(i)\n",
      "    print('正在检查是否上传至37网盘')\n",
      "    for i in file7:\n",
      "        if os.path.exists(os.path.join(path37,i+YY)):\n",
      "            print(i+'存在，不再进行备份')\n",
      "        else:\n",
      "            print(i+'文件还未备份，开始创建备份')\n",
      "            file7bf.append(i)\n",
      "            \n",
      "    for bf in file7bf:  #获得需要备份文件夹名称\n",
      "        for a,b,c in os.walk(Y): #获得原本文件夹名称\n",
      "            if bf in a:  #如果备份文件夹名称在原本文件夹名称 列表内，则\n",
      "                try:\n",
      "                    print('开始备份，请稍后')\n",
      "                    shutil.copytree(a,path37+'\\\\'+bf+YY) #开始备份\n",
      "                except FileExistsError:\n",
      "                    for filepath in os.scandir(a):\n",
      "                        shutil.copy2(filepath.path,path37+'\\\\'+bf+YY)\n",
      "                    print('备份完成')\n",
      "                    # a = y:Sun\\20201027_014029\n",
      "            elif bf[6:8]+bf[4:6]+bf[2:4] in a: #针对2020年10月24日之前的文件命名\n",
      "                print('开始备份，请稍后')\n",
      "                shutil.copytree(a,path37+'\\\\'+bf+YY) #开始备份\n",
      "                print('备份完成')\n",
      "history\n"
     ]
    }
   ],
   "source": [
    "history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "set()"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "set(file7bf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#测试\n",
    "for bf in set(file7bf):  #获得需要备份文件夹名称\n",
    "    for i in set(os.scandir()): #获得原本文件夹名称\n",
    "        if bf in i.name:  #如果备份文件夹名称在原本文件夹名称 列表内，则\n",
    "            print(i.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    " for bf in file7bf:  #获得需要备份文件夹名称\n",
    "        for i in os.scandir(): #获得原本文件夹名称\n",
    "            if bf in i.name:  #如果备份文件夹名称在原本文件夹名称 列表内，则\n",
    "                try:\n",
    "                    print('开始备份，请稍后')\n",
    "                    move = os.path.join(Y,i.name)  #获得原本地址\n",
    "                    #print(path37+'\\\\'+i.name[:8]+YY)   \\\\10.153.110.37\\Data\\太阳观测\\2020\\cak\\20201024CAK\n",
    "                    shutil.copytree(move,path37+'\\\\'+i.name[:8]+YY) #开始备份\n",
    "                except FileExistsError:\n",
    "                    for filepath in os.scandir(os.path.join(Y,i.name)):\n",
    "                        shutil.copy2(filepath.path,path37+'\\\\'+i.name[:8]+YY)\n",
    "                    print('备份完成')\n",
    "                \n",
    "            elif bf[6:8]+bf[4:6]+bf[2:4] in i.name: #针对2020年10月24日之前的文件命名\n",
    "                print('开始备份，请稍后')\n",
    "                move = os.path.join(Y,i.name)  #获得原本地址\n",
    "                shutil.copytree(move,path37+'\\\\'+bf+YY) #开始备份\n",
    "                print('备份完成')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2021年1月4日备份\n",
    "\n",
    "import os\n",
    "import datetime,shutil\n",
    "pan = ['X','Z']\n",
    "for p in pan:\n",
    "    if p == 'X': #第一次检查 49 wl电脑文件\n",
    "        Y = r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl'\n",
    "        YY ='WL'\n",
    "        path37 =r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\wl'\n",
    "    elif p == 'Z':  #第二次检查 46 cak电脑文件\n",
    "        Y = r'Z:'\n",
    "        YY = 'CAK'\n",
    "        path37 = r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\cak'\n",
    "        \n",
    "    filelie=[]#文件列表\n",
    "    for root,dirs,files in os.walk(Y):\n",
    "        for i in dirs:\n",
    "            if len(i) == 13:  #针对10/20之前的文件命名\n",
    "                times ='20'+i[4:6]+i[2:4]+i[0:2]\n",
    "                #times=datetime.datetime.strptime(times,'%Y%m%d')\n",
    "                filelie.append(times)\n",
    "            elif len(i) >13:  #新的文件命名方式\n",
    "                filelie.append(i[:8])\n",
    "    #对文件夹进行排列\n",
    "    filelie.sort(reverse=True)\n",
    "    \n",
    "    file7 =[] #输出最近7天观测时间 20201203\n",
    "    file7bf=[] #备份文件名称 20201203\n",
    "\n",
    "    #如需修改观测时间列表长度，则修改for i in filelie[:7]尾部[:7]的7，需要改成10则是[:10]\n",
    "    print(Y +'盘最近15次观测的时间列表')\n",
    "    for i in filelie[:15]:\n",
    "        file7.append(i)\n",
    "        print(i)\n",
    "    print('正在检查是否上传至37网盘')\n",
    "    for i in file7:\n",
    "        if os.path.exists(os.path.join(path37,i+YY)):\n",
    "            print(i+'存在，不再进行备份')  #20201203\n",
    "        else:\n",
    "            print(i+'文件还未备份，开始创建备份')\n",
    "            file7bf.append(i)  #20201203\n",
    "    \n",
    "    os.chdir(Y)\n",
    "    for bf in file7bf:  #获得需要备份文件夹名称\n",
    "        for i in set(os.scandir()): #获得原本文件夹名称\n",
    "            if bf in i.name:  #如果备份文件夹名称在原本文件夹名称 列表内，则\n",
    "                try:\n",
    "                    print('开始备份1，请稍后')\n",
    "                    print(i.name)\n",
    "                    move = os.path.join(Y,i.name)  #获得原本地址\n",
    "                    if i.name[:4] == '2020':\n",
    "                        yy=YY.lower()\n",
    "                        #地址 = r'\\\\10.153.110.37\\Data\\solar_observation\\2020\\'+ yy\n",
    "                        地址 = os.path.join(r'\\\\10.153.110.37\\Data\\solar_observation\\2020',yy)\n",
    "                    elif i.name[:4] == '2021':\n",
    "                        地址 = path37\n",
    "                        #print(path37+'\\\\'+i.name[:8]+YY)   \\\\10.153.110.37\\Data\\太阳观测\\2020\\cak\\20201024CAK\n",
    "                    shutil.copytree(move,地址+'\\\\'+i.name[:8]+YY) #开始备份\n",
    "                    print(i.name[:8]+'备份完成')\n",
    "                except FileExistsError:  \n",
    "                    for filepath in os.scandir(os.path.join(Y,i.name)):\n",
    "                        shutil.copy2(filepath.path,path37+'\\\\'+i.name[:8]+YY)\n",
    "                    print(i.name[:8]+'备份完成')\n",
    "                except SameFileError:\n",
    "                    pass\n",
    "                \n",
    "            elif bf[6:8]+bf[4:6]+bf[2:4] in i.name: #针对2020年10月24日之前的文件命名\n",
    "                try:\n",
    "                    print('开始备份2，请稍后')\n",
    "                    move = os.path.join(Y,i.name)  #获得原本地址\n",
    "                    shutil.copytree(move,path37+'\\\\'+bf+YY) #开始备份\n",
    "                    print('备份完成')\n",
    "                except FileExistsError:\n",
    "                    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
